﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using GBFinance.Entity;
using HiLand.Framework.FoundationLayer;
using HiLand.Utility.DataBase;
using HiLand.Utility.Enums;
using HiLand.Utility.Finance;

namespace GBFinance.DAL
{
    public class LoanWorkDAL : BaseSqlDAL<LoanWorkEntity>
    {
        #region 基本信息
        /// <summary>
        /// 实体对应主表的名称
        /// </summary>
        protected override string TableName
        {
            get { return "LoanWorks"; }
        }

        /// <summary>
        /// 主键名称
        /// </summary>
        protected override string[] KeyNames
        {
            get { return new string[] { "WorkGuid" }; }
        }

        /// <summary>
        /// Guid主键名称
        /// </summary>
        protected override string GuidKeyName
        {
            get { return  "WorkGuid" ; }
        }

        /// <summary>
        /// 分页存储过程的名字
        /// </summary>
        protected override string PagingSPName
        {
            get { return "usp_General_LoanWorks_SelectPaging"; }
        }
        #endregion

        #region 逻辑操作
        public override bool Create(LoanWorkEntity entity)
        {
            string commandText = @"Insert Into [LoanWorks] (
			[WorkGuid],
			[UserGuid],
			[EmployerName],
			[EmployerAddress],
			[EmployerTelephone],
			[EmploymentStatus],
			[JobTitle],
			[TimeEmployedYears],
			[TimeEmployedMonths],
			[IncomeafterTaxes],
			[IncomeCircle],
			[NextIncomeDay],
			[MainPayment],
			[RegularOtherPayment],
			[MainPaymentCircle],
			[RegularOtherPaymentCircle],
			[PropertyNames],
			[PropertyValues]
        ) 
        Values (
			@WorkGuid,
			@UserGuid,
			@EmployerName,
			@EmployerAddress,
			@EmployerTelephone,
			@EmploymentStatus,
			@JobTitle,
			@TimeEmployedYears,
			@TimeEmployedMonths,
			@IncomeafterTaxes,
			@IncomeCircle,
			@NextIncomeDay,
			@MainPayment,
			@RegularOtherPayment,
			@MainPaymentCircle,
			@RegularOtherPaymentCircle,
			@PropertyNames,
			@PropertyValues
        )";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.Instance.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }

        public override bool Update(LoanWorkEntity entity)
        {
            string commandText = @"Update [LoanWorks] Set   
					[WorkGuid] = @WorkGuid,
					[UserGuid] = @UserGuid,
					[EmployerName] = @EmployerName,
					[EmployerAddress] = @EmployerAddress,
					[EmployerTelephone] = @EmployerTelephone,
					[EmploymentStatus] = @EmploymentStatus,
					[JobTitle] = @JobTitle,
					[TimeEmployedYears] = @TimeEmployedYears,
					[TimeEmployedMonths] = @TimeEmployedMonths,
					[IncomeafterTaxes] = @IncomeafterTaxes,
					[IncomeCircle] = @IncomeCircle,
					[NextIncomeDay] = @NextIncomeDay,
					[MainPayment] = @MainPayment,
					[RegularOtherPayment] = @RegularOtherPayment,
					[MainPaymentCircle] = @MainPaymentCircle,
					[RegularOtherPaymentCircle] = @RegularOtherPaymentCircle,
					[PropertyNames] = @PropertyNames,
					[PropertyValues] = @PropertyValues
             Where [WorkGuid] = @WorkGuid";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.Instance.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }

        #endregion


        #region 辅助方法
        protected override void InnerPrepareParasAll(LoanWorkEntity entity, ref List<SqlParameter> paraList)
        {
            List<SqlParameter> list = new List<SqlParameter>() 
            {
			    new SqlParameter("@WorkGuid",entity.WorkGuid== Guid.Empty?Guid.NewGuid():entity.WorkGuid),
			    new SqlParameter("@UserGuid",entity.UserGuid),
			    new SqlParameter("@EmployerName",entity.EmployerName??string.Empty),
			    new SqlParameter("@EmployerAddress",entity.EmployerAddress??string.Empty),
			    new SqlParameter("@EmployerTelephone",entity.EmployerTelephone??string.Empty),
			    new SqlParameter("@EmploymentStatus",(int)entity.EmploymentStatus),
			    new SqlParameter("@JobTitle",entity.JobTitle??string.Empty),
			    new SqlParameter("@TimeEmployedYears",entity.TimeEmployedYears),
			    new SqlParameter("@TimeEmployedMonths",entity.TimeEmployedMonths),
			    new SqlParameter("@IncomeafterTaxes",entity.IncomeafterTaxes),
			    new SqlParameter("@IncomeCircle",(int)entity.IncomeCircle),
			    new SqlParameter("@NextIncomeDay",entity.NextIncomeDay),
			    new SqlParameter("@MainPayment",entity.MainPayment),
			    new SqlParameter("@RegularOtherPayment",entity.RegularOtherPayment),
			    new SqlParameter("@MainPaymentCircle",(int)entity.MainPaymentCircle),
			    new SqlParameter("@RegularOtherPaymentCircle",(int)entity.RegularOtherPaymentCircle)
            };

            paraList.AddRange(list);
        }

        protected override LoanWorkEntity Load(IDataReader reader)
        {
            LoanWorkEntity entity = new LoanWorkEntity();
            if (reader != null && reader.IsClosed == false)
            {
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "WorkID"))
                {
                    entity.WorkID = reader.GetInt32(reader.GetOrdinal("WorkID"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "WorkGuid"))
                {
                    entity.WorkGuid = reader.GetGuid(reader.GetOrdinal("WorkGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "UserGuid"))
                {
                    entity.UserGuid = reader.GetGuid(reader.GetOrdinal("UserGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "EmployerName"))
                {
                    entity.EmployerName = reader.GetString(reader.GetOrdinal("EmployerName"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "EmployerAddress"))
                {
                    entity.EmployerAddress = reader.GetString(reader.GetOrdinal("EmployerAddress"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "EmployerTelephone"))
                {
                    entity.EmployerTelephone = reader.GetString(reader.GetOrdinal("EmployerTelephone"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "EmploymentStatus"))
                {
                    entity.EmploymentStatus = (JobStatuses)reader.GetInt32(reader.GetOrdinal("EmploymentStatus"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "JobTitle"))
                {
                    entity.JobTitle = reader.GetString(reader.GetOrdinal("JobTitle"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "TimeEmployedYears"))
                {
                    entity.TimeEmployedYears = reader.GetInt32(reader.GetOrdinal("TimeEmployedYears"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "TimeEmployedMonths"))
                {
                    entity.TimeEmployedMonths = reader.GetInt32(reader.GetOrdinal("TimeEmployedMonths"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "IncomeafterTaxes"))
                {
                    entity.IncomeafterTaxes = reader.GetDecimal(reader.GetOrdinal("IncomeafterTaxes"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "IncomeCircle"))
                {
                    entity.IncomeCircle = (PaymentTermTypes)reader.GetInt32(reader.GetOrdinal("IncomeCircle"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "NextIncomeDay"))
                {
                    entity.NextIncomeDay = reader.GetDateTime(reader.GetOrdinal("NextIncomeDay"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "MainPayment"))
                {
                    entity.MainPayment = reader.GetDecimal(reader.GetOrdinal("MainPayment"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "RegularOtherPayment"))
                {
                    entity.RegularOtherPayment = reader.GetDecimal(reader.GetOrdinal("RegularOtherPayment"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "MainPaymentCircle"))
                {
                    entity.MainPaymentCircle = (PaymentTermTypes)reader.GetInt32(reader.GetOrdinal("MainPaymentCircle"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "RegularOtherPaymentCircle"))
                {
                    entity.RegularOtherPaymentCircle = (PaymentTermTypes)reader.GetInt32(reader.GetOrdinal("RegularOtherPaymentCircle"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyNames"))
                {
                    entity.PropertyNames = reader.GetString(reader.GetOrdinal("PropertyNames"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyValues"))
                {
                    entity.PropertyValues = reader.GetString(reader.GetOrdinal("PropertyValues"));
                }
            }
            return entity;
        }
        #endregion
    }
}